﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Data;

public partial class ControlUser_QuetMaKhiNhapHang : System.Web.UI.UserControl
{
    clsObj Obj;
    clsSql Sql = new clsSql();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            imgXoa.Attributes.Add("OnClick", "return confirm('Bạn chắc chắn muốn xóa những mục đã chọn?')");
            fload_LoaiNghiepVu();
            // làm già đó thì làm 
        } 
    }
    protected void txtHoaDon_TextChanged(object sender, EventArgs e)
    {
        fload_HoaDon();
       
    }
    void fload_HoaDon()
    {
        Obj = new clsObj();
        Obj.Parameter = new string[] { "@InDocNo", "@Username", "@trnType" };
        Obj.ValueParameter = new object[] { txtHoaDon.Text, Session["username"].ToString(),ddlLoaiNghiepVu.SelectedValue };
        Obj.SpName = "ls_isExistedInDoc4Scan";
        Sql.fGetData(Obj);
        if (Obj.Dt.Rows.Count > 0)
        {
            fload_ChiTietHoaDon();
            Page.SetFocus(txtMaVach);
             txtMaVach.Focus();
        }
        else
        {
            uscMsgBox1.AddMessage("Không tồn tại số hóa đơn này", uscMsgBox.enmMessageType.Error);
            txtSoLanQuetSau.Text = "";
            txtSoLanChoQuet.Text = "";
            txtSoLanDaQuet.Text ="";
            txtSoLanPhaiQuet.Text = "";
            txtHoaDon.Text = "";
        }
    }

    private void fload_ChiTietHoaDon()
    {
        clsObj Obj1 = new clsObj();
        Obj1.Parameter = new string[] { "@InDocNo","@trnType" };
        Obj1.ValueParameter = new object[] { txtHoaDon.Text,ddlLoaiNghiepVu.SelectedValue };
        Obj1.SpName = "ls_getScanInDocInfo";
        Sql.fGetData(Obj1);
        if (Obj1.Dt.Rows.Count > 0)
        {
            txtSoLanQuetSau.Text = Obj1.Dt.Rows[0]["LaterScanned"].ToString();
            txtSoLanChoQuet.Text = Obj1.Dt.Rows[0]["WillBeScanned"].ToString();
            txtSoLanDaQuet.Text = Obj1.Dt.Rows[0]["HaveScanned"].ToString();
            txtSoLanPhaiQuet.Text = Obj1.Dt.Rows[0]["mustBeScanned"].ToString();
        }
        else
        {
            txtSoLanQuetSau.Text = "0";
            txtSoLanChoQuet.Text = "0";
            txtSoLanDaQuet.Text = "0";
            txtSoLanPhaiQuet.Text = "0";
        }
    }

    void fload_Mavach()
    {

        Obj = new clsObj();
        Obj.Parameter = new string[] { "@ScannedCode" ,"@siteCode","@trnType" };
        Obj.ValueParameter = new object[] { txtMaVach.Text, Session["Sitecode"].ToString(),ddlLoaiNghiepVu.SelectedValue };
        Obj.SpName = "ls_isCode4InInvValid";
        Sql.fGetData(Obj);
        if (Obj.Dt.Rows.Count <= 0)
        {
            uscMsgBox1.AddMessage("Mã vạch này không hợp lệ", uscMsgBox.enmMessageType.Error);
            txtMaVach.Text = "";
        }
        else
        {
            //@scannedCode varchar(10),@userName varchar(50), @InDocNo
            clsObj Obj1 = new clsObj();
            Obj1.Parameter = new string[] { "@scannedCode", "@userName", "@InDocNo", "@trnType" };
           Obj1.ValueParameter = new object[] { txtMaVach.Text,Session["username"].ToString(),txtHoaDon.Text,ddlLoaiNghiepVu.SelectedValue};
           Obj1.SpName = "ls_saveScanCode4InInv";
           Sql.fNonGetData(Obj1);
           txtMaVach.Text = "";
           txtMaVach.Focus();
        }
    }

    void fload_LoaiNghiepVu()
    {

        Obj = new clsObj();
        Obj.Parameter = new string[] { "@FlgIn"};
        Obj.ValueParameter = new object[] { 1 };
        Obj.SpName = "ls_getIOCodeType";
        Sql.fGetData(Obj);
        if (Obj.Dt.Rows.Count >= 0)
        {
            ddlLoaiNghiepVu.DataSource = Obj.Dt;
            ddlLoaiNghiepVu.DataTextField = "mota";
            ddlLoaiNghiepVu.DataValueField = "ma";
            ddlLoaiNghiepVu.DataBind();
        }
       
    }
    protected void txtMaVach_TextChanged(object sender, EventArgs e)
    {
        fload_Mavach();
        fload_ChiTietHoaDon();
    }
    protected void imgXoa_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            clsObj Obj1 = new clsObj();
            Obj1.Parameter = new string[] { "@InDocNo","@trnType" };
            Obj1.ValueParameter = new object[] { txtHoaDon.Text,ddlLoaiNghiepVu.SelectedValue };
            Obj1.SpName = "ls_deleteScanInfo4InDoc";
            Sql.fNonGetData(Obj1);
            fload_ChiTietHoaDon();
        }
        finally
        {
            uscMsgBox1.AddMessage("Đã Xóa Thành Công", uscMsgBox.enmMessageType.Error);
        }
    }
    protected void ddlLoaiNghiepVu_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtMaVach.Focus();
    }
}
